﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class GeneralLedgerDetailDA : IGeneralLedgerDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string GENERAL_LEDGER_ID = "@GENERAL_LEDGER_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string PERCENT = "@PERCENT";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_GENERAL_LEDGER_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@GENERAL_LEDGER_DETAIL WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_GENERAL_LEDGER_DETAIL = " DELETE FROM @_@GENERAL_LEDGER_DETAIL WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID";
        private string SQL_INSERT_GENERAL_LEDGER_DETAIL = " INSERT INTO @_@GENERAL_LEDGER_DETAIL ( WH_ID, GENERAL_LEDGER_ID, LINE_ID, SUBJECT_ID, REMARK, [PERCENT], IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @GENERAL_LEDGER_ID, @LINE_ID, @SUBJECT_ID, @REMARK, @PERCENT, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL = " SELECT  ROWID, WH_ID, GENERAL_LEDGER_ID, LINE_ID, SUBJECT_ID, REMARK, [PERCENT], IS_ACTIVE,TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@GENERAL_LEDGER_DETAIL ";
        private string SQL_UPDATE_GENERAL_LEDGER_DETAIL = " UPDATE @_@GENERAL_LEDGER_DETAIL SET WH_ID = @WH_ID, SUBJECT_ID = @SUBJECT_ID, REMARK = @REMARK, [PERCENT] = @PERCENT, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID AND LINE_ID = @LINE_ID  ";
        private string SUBJECT_ID = "@SUBJECT_ID";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public DataSet CheckGeneralLedgerDetailIDBySubjectID(string subjectID, string whLoginID)
        {
            string sql = " SELECT COUNT(1) FROM @_@GENERAL_LEDGER_DETAIL WHERE SUBJECT_ID = @SUBJECT_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SUBJECT_ID", subjectID) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql.Replace("@_@", whLoginID), paras);
        }

        public DataSet CheckGeneralLedgerDetailIDBySubjectID(DataBase dataBase, DbTransaction tran, string subjectID, string whLoginID)
        {
            string sql = " SELECT COUNT(1) FROM @_@GENERAL_LEDGER_DETAIL WHERE SUBJECT_ID = @SUBJECT_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SUBJECT_ID", subjectID) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql.Replace("@_@", whLoginID), paras);
        }

        public bool CheckGeneralLedgerDetailIDUnique(string generalLedgerID, int lienID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_GENERAL_LEDGER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckGeneralLedgerDetailIDUnique(DataBase dataBase, DbTransaction tran, string generalLedgerID, int lienID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_GENERAL_LEDGER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteGeneralLedgerDetail(string generalLedgerID, List<string> lienID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                string strSql = this.SQL_DELETE_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID) + " AND LINE_ID = @LINE_ID ";
                for (int j = 0; j < lienID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteGeneralLedgerDetail(DataBase dataBase, DbTransaction tran, string generalLedgerID, string whLoginID)
        {
            int result = 0;
            try
            {
                string strSql = this.SQL_DELETE_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID);
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteGeneralLedgerDetail(DataBase dataBase, DbTransaction tran, string generalLedgerID, List<string> lienID, string whLoginID)
        {
            int result = 0;
            try
            {
                string strSql = this.SQL_DELETE_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID) + " AND LINE_ID = @LINE_ID ";
                for (int j = 0; j < lienID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllGeneralLedgerDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllGeneralLedgerDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<GeneralLedgerDetailInfo> GetGeneralLedgerDetailByID(string generalLedgerID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID) + " WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID) };
            List<GeneralLedgerDetailInfo> generalLedgerDetailList = new List<GeneralLedgerDetailInfo>();
            GeneralLedgerDetailInfo generalLedgerDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    generalLedgerDetailInfo = new GeneralLedgerDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), Convert.ToInt32(reader["LINE_ID"]), reader["SUBJECT_ID"].ToString(), reader["REMARK"].ToString(), (reader["PERCENT"].ToString() != "") ? decimal.Parse(reader["PERCENT"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    generalLedgerDetailList.Add(generalLedgerDetailInfo);
                }
            }
            return generalLedgerDetailList;
        }

        public GeneralLedgerDetailInfo GetGeneralLedgerDetailByID(string generalLedgerID, int lienID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID) + " WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID) };
            GeneralLedgerDetailInfo generalLedgerDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    generalLedgerDetailInfo = new GeneralLedgerDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), Convert.ToInt32(reader["LINE_ID"]), reader["SUBJECT_ID"].ToString(), reader["REMARK"].ToString(), (reader["PERCENT"].ToString() != "") ? decimal.Parse(reader["PERCENT"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return generalLedgerDetailInfo;
        }

        public GeneralLedgerDetailInfo GetGeneralLedgerDetailByID(DataBase dataBase, DbTransaction tran, string generalLedgerID, int lienID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID) + " WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID), new SqlParameter("@LINE_ID", lienID) };
            GeneralLedgerDetailInfo generalLedgerDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                generalLedgerDetailInfo = new GeneralLedgerDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), Convert.ToInt32(reader["LINE_ID"]), reader["SUBJECT_ID"].ToString(), reader["REMARK"].ToString(), (reader["PERCENT"].ToString() != "") ? decimal.Parse(reader["PERCENT"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return generalLedgerDetailInfo;
        }

        public DataSet GetGeneralLedgerDetailByQueryList(List<string> sqlWhere, GeneralLedgerDetailQueryEntity generalledgerdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!generalledgerdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, generalledgerdetailQuery.CurrentPage, generalledgerdetailQuery.PageSize, generalledgerdetailQuery.SortField, generalledgerdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetGeneralLedgerDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, GeneralLedgerDetailQueryEntity generalledgerdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, generalledgerdetailQuery.CurrentPage, generalledgerdetailQuery.PageSize, generalledgerdetailQuery.SortField, generalledgerdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertGeneralLedgerDetail(GeneralLedgerDetailInfo generalLedgerDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                generalLedgerDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedgerDetail_Parameters(generalLedgerDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertGeneralLedgerDetail(DataBase dataBase, DbTransaction tran, GeneralLedgerDetailInfo generalLedgerDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                generalLedgerDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedgerDetail_Parameters(generalLedgerDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_GeneralLedgerDetail_Parameters(GeneralLedgerDetailInfo generalLedgerDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.GENERAL_LEDGER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int), new SqlParameter(this.SUBJECT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.PERCENT, SqlDbType.Decimal, 5), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1) };
            paramArray[0].Value = generalLedgerDetailInfo.WhID;
            paramArray[1].Value = generalLedgerDetailInfo.GeneralLedgerID;
            paramArray[2].Value = generalLedgerDetailInfo.LineID;
            paramArray[3].Value = generalLedgerDetailInfo.SubjectID;
            if (!string.IsNullOrEmpty(generalLedgerDetailInfo.Remark))
            {
                paramArray[4].Value = generalLedgerDetailInfo.Remark;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = generalLedgerDetailInfo.Percent;
            paramArray[6].Value = generalLedgerDetailInfo.CreatedBy;
            paramArray[7].Value = generalLedgerDetailInfo.UpdatedBy;
            paramArray[8].Value = generalLedgerDetailInfo.IsActive;
            return paramArray;
        }

        public int UpdateGeneralLedgerDetail(GeneralLedgerDetailInfo generalLedgerDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                generalLedgerDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedgerDetail_Parameters(generalLedgerDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateGeneralLedgerDetail(DataBase dataBase, DbTransaction tran, GeneralLedgerDetailInfo generalLedgerDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                generalLedgerDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedgerDetail_Parameters(generalLedgerDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GENERAL_LEDGER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

